<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category extends BaseController {
	var $numNodes = 10;
	
	var $cache_time = 0;
	var $cache_time_nodes = 0;
	
	function Category(){
		parent::BaseController();
		
		$this->load->model("Node_Model", "Nodes");
		$this->load->model("Category_Model", "Categories");

		$this->load->helper('date');
		$this->template->addData("date_format", "%d/%m/%Y");
		
		$cache_time = $this->settings->item("_site_cache_categories_time");
		if($cache_time != NULL){
			$this->cache_time = intval($cache_time);
		}
		
		$cache_time_nodes = $this->settings->item("_site_cache_nodes_time");
		if($cache_time_nodes != NULL){
			$this->cache_time_nodes = intval($cache_time_nodes);
		}
	}
	
	function _remap($category_name = NULL){
		//si existe el metodo, lo llamamos, si no es que estamos llamando a una categoria determinada
		if($category_name != "index"){
			$this->index($category_name);
		} else {
			$this->list_all();
		} 
	}

	function index($category_identifier = NULL){
		
		$this->template->setPage("category/index");
		
		if($category_identifier != NULL && $category_identifier != ""){
			
			$category = NULL;
						
			if(is_numeric($category_identifier)){
				$category = $this->mp_cache->model("Categories", "get", array(array("id" => $category_identifier)), $this->cache_time);
			} else{
				$category = $this->mp_cache->model("Categories", "get", array(array("url_name" => $category_identifier)), $this->cache_time);
			}
			
			if($category != NULL){
				$this->template->addData("category", $category);
				
				$metas = $this->mp_cache->model("Categories", "getMeta", array($category->id), $this->cache_time);
				$this->template->addData("metas", $metas);
				
				
				$childrens_categories = $this->mp_cache->model("Categories", "get", array(array("parent" => $category->id)), $this->cache_time);
				$this->template->addData("childrens_categories", $childrens_categories);
				
				//Obtenemos los nodos
				$nodes_params = array(
					"categories" => $category->id, 
					"categories_childs" => TRUE
				);
				//incluimos los nodos de las categorias hijas
				$nodes = $this->mp_cache->model("Nodes", "get", array($nodes_params), $this->cache_time_nodes);
				$this->template->addData("nodes", $nodes);
				
				$totalNodes = $this->mp_cache->model("Nodes", "getCount", array($nodes_params), $this->cache_time_nodes);
				$this->template->addData("totalNodes", $totalNodes);
				
				$this->load->library('pagination');

				$config['base_url'] = site_url("/category/");
				$config['total_rows'] = $totalNodes;
				$config['per_page'] = $this->numNodes;
				$config['uri_segment'] = 2;
				$config['first_link'] = '&laquo; Inicio';
				$config['last_link'] = 'Final &raquo;';
				$config['prev_link'] = '&laquo;';
				$config['next_link'] = '&raquo;';
		
				$this->pagination->initialize($config);
				
				$this->template->render();
			} else{
				//redirect("error/error_404");
				//show_404();
			}
		} else{
			//redirect("error/error_404");
			//show_404();
		}
	}
	
	function list_all(){
		$this->template->setPage("category/list_all");
		
		$categories = $this->mp_cache->model("Categories", "get", array(), $this->cache_time);
		$categories = $this->_get_categories_extra_info($categories);
		
		$this->template->addData("categories", $categories);	
		
		$this->template->render();
	}
	
	function _get_categories_extra_info($categories = NULL){
		$process_categories = array();
		
		if($categories != NULL){
			foreach($categories as $category){
				$category_metas = $this->mp_cache->model("Categories", "getMeta", array("id" => $category->id), $this->cache_time);
				$category->metas = $category_metas;
				
				//Obtenemos los nodos.
				$category_nodes = $this->mp_cache->model("Nodes", "get", array(array("categories" => $category->id, "start" => 0, "offset" => 5)), $this->cache_time_nodes);
				$category->nodes = $category_nodes;
				
				$category_num_nodes = $this->mp_cache->model("Nodes", "getCount", array(array("categories" => $category->id)));
				$category->num_nodes = $category_num_nodes;
				
				$category_all_nodes = $this->mp_cache->model("Nodes", "get", array(array("categories" => $category->id, "categories_childs" => TRUE, "start" => 0, "offset" => 5)), $this->cache_time_nodes);
				$category->all_nodes = $category_all_nodes;
				
				$category_all_num_nodes = $this->mp_cache->model("Nodes", "getCount", array(array("categories" => $category->id, "categories_childs" => TRUE)), $this->cache_time_nodes);
				$category->num_all_nodes = $category_all_num_nodes;
				
				
				if(isset($category->categories)){
					$childrens_categories = $this->_get_categories_extra_info($category->categories);
					
					$category->categories = $childrens_categories;
				}
				
				array_push($process_categories, $category);
			}
		}
		
		return $process_categories;
	}
	
}
?>
